package com.dux.shiro1.config;

import com.dux.shiro1.domin.User;
import com.dux.shiro1.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class NameRealm extends AuthorizingRealm {

    @Autowired
    private UserService userService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        String user = getAvailablePrincipal(principals).toString();


        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken upt = (UsernamePasswordToken) token;
        User user = userService.getUser(upt.getUsername(), String.copyValueOf(upt.getPassword()));

        if (user == null){
            throw new UnknownAccountException("用户信息不正确");
        }

        return new SimpleAuthenticationInfo(upt.getUsername(), upt.getPassword(),
                ByteSource.Util.bytes(user.getSalt()), getName());
    }
}
